package com.qxsx.midi.activity.exam;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.main.MainActivity;
import com.qxsx.midi.web.SWebViewSetting;

/**
 * Created by Joney on 2015/9/15.
 */
public class VideoPlayActivity extends Activity implements SensorEventListener {
    private WebView webView;
    private FrameLayout video_fullView;
    private SensorManager sensorManager;
    private String lesson_url;

    @Override
    protected void onResume() {
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
        super.onResume();
        //恢复播放
        webView.resumeTimers();
    }

    @Override
    protected void onPause() {
        // acitivity后台时取消监听
        sensorManager.unregisterListener(this);
        super.onPause();
        //暂停播放
        webView.pauseTimers();
    }


    @Override
    protected void onDestroy() {
        sensorManager.unregisterListener(this);
        super.onDestroy();
        //一定要销毁，否则无法停止播放
        webView.destroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //判断是否是横屏
//            if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
//                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//            } else {
//                return true;
//            }

            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                finish();
            }
            return true;
        }
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉应用标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        Intent intent = getIntent();
        lesson_url = intent.getStringExtra("lesson_url");//得到lesson_url
        Log.w("lesson_url", lesson_url);
//
//        String html5 = "<!DOCTYPE html>\n" +
//                "<html>\n" +
//                "<head>\n" +
//                "\t<title></title>\n" +
//                "</head>\n" +
//                "<body>\n" +
//                //lesson_url +
//                "<iframe\n" +
//                "src=\"http://www.tudou.com/programs/view/html5embed.action?code=kbPzDzCIeBE&autoPlay=true&playType=H5\"\n" +
//                "width=\"100%\"\n" +
//                "height=\"524px\"\n" +
//                "frameborder=\"0\"\n" +
//                "scrolling=\"no\"\n" +
//                "></iframe>\n" +
//                "</body>\n" +
//                "</html>";


        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        webView = (WebView) findViewById(R.id.webView);
        video_fullView = (FrameLayout) findViewById(R.id.video_fullView);
        //支持全屏播放
        SWebViewSetting setting = new SWebViewSetting(webView, VideoPlayActivity.this);
        setting.supportVideoFullScreen(video_fullView);
        webView = setting.build();


//        //加载网页
//        webView.loadUrl("http://v.qq.com/iframe/player.html?vid=e01264pnkg9&tiny=0&auto=0");
        webView.loadUrl(lesson_url.replaceAll("\\<iframe +src=\"(.*?)\".*?\\>\\ *</iframe\\>", "$1"));
        //webView.loadDataWithBaseURL(null, lesson_url, "text/html", "utf-8", null);
        // webView.loadData(lesson_url, "text/html", "utf-8");
//        webView.loadUrl("http://www.tudou.com/albumplay/YkXp849rWok/0MoWF_g8S5c.html");

    }


    String TAG = "sumile";

    @Override
    public void onSensorChanged(SensorEvent event) {
        final int sensorType = event.sensor.getType();
        //values[0]:X轴，values[1]：Y轴，values[2]：Z轴
        final float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];
        x = Math.abs(x);
        y = Math.abs(y);
        z = Math.abs(z);
        if ((x >= 6 && z <= 6) || (x >= 6 && y >= 6)) {
            Log.i(TAG, "heng" + x + "--" + y + "--" + z);
            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            }
        } else if ((x <= 3 && z <= 6) || (z <= 6 && y <= 3)) {
            Log.i(TAG, "shu" + x + "--" + y + "--" + z);
            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}